<?php
/**
 * @title 2462. 雇佣 K 位工人的总代价 - 最小堆
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $costs
     * @param Integer $k
     * @param Integer $candidates
     * @return Integer
     */
    function totalCost($costs, $k, $candidates) {
        /**
         * @since 2024-05-06 元素数量
         */
        $count = count($costs);

        /**
         * @since 2024-05-06 前candidates个左边最小堆，后candidates个右边最小堆
         */
        $left = $candidates-1;
        $right = max($left+1,$count - $candidates);

        /**
         * @since 2024-05-06 左边最小堆
         */
        $leftHeap = new SplMinHeap();
        for ($i=0; $i<=$left; $i++){
            $leftHeap->insert($costs[$i]);
        }

        /**
         * @since 2024-05-06 右边最小堆
         */
        $rightHeap = new SplMinHeap();
        for ($i=$right; $i<$count; $i++){
            $rightHeap->insert($costs[$i]);
        }

        /**
         * @since 2024-05-06 总代价
         */
        $totalCost = 0;
        for($i=0; $i<$k; $i++){
            /**
             * @since 2024-05-06 左边最小堆最小值，右边最小堆最小值
             */
            if($leftHeap->isEmpty()){
                $leftTop = PHP_INT_MAX;
            } else {
                $leftTop = $leftHeap->top();
            }
            if($rightHeap->isEmpty()){
                $rightTop = PHP_INT_MAX;
            } else {
                $rightTop = $rightHeap->top();
            }

            /**
             * @since 2024-05-06 左边元素出堆
             */
            if($leftTop <= $rightTop){
                $totalCost += $leftTop;
                $leftHeap->extract();

                /**
                 * @since 2024-05-06 left+1指针元素入堆
                 */
                if($left+1 < $right){
                    $left++;
                    $leftHeap->insert($costs[$left]);
                } else {}
            } else {
                /**
                 * @since 2024-05-06 右边元素出堆
                 */
                $totalCost += $rightTop;
                $rightHeap->extract();

                /**
                 * @since 2024-05-06 right-1指针元素入堆
                 */
                if($right-1 > $left){
                    $right--;
                    $rightHeap->insert($costs[$right]);
                } else {}
            }
        }

        /**
         * @return Integer
         */
        return $totalCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.01.php
 */
$datas = [
    // [[17,12,10,2,7,2,11,20,8], 3, 4], // 11
    [[69,10,63,24,1,71,55,46,4,61,78,21,85,52,83,77,42,21,73,2,80,99,98,89,55,94,63,50,43,62,14], 21, 31], // 829
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->totalCost(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
